package com.ezscript.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 获取文件中以start_str 开始，end_str结束的字符串
 * @author MaXin
 */
public class CatchString {
	
	public static String src_file = "d:\\temp\\db_log.log";
	public static String start_str="INSERT INTO";
	public static String end_str=";";
	
	
	public static void main(String [] args){
		//replaceFileByLines(src_file,target_file);
//		catchFile(src_file,start_str,end_str);
		readFile(src_file);
	}
	
	
    private static void catchFile(String fileName, String start,
			String end) {
    	File file = new File(fileName);
    	BufferedReader reader = null;
        try {
            System.out.println("截取文件内容：以'"+start+"'开始，以'"+end+"'结束");
            reader = new BufferedReader(new InputStreamReader( new FileInputStream(file),"utf-8"));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
            	if(tempString.startsWith(start)){
            		do {
						System.out.println(tempString);
						if(tempString.endsWith(end))
							break;
					} while ((tempString = reader.readLine()) != null );
            	}
            }
            
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
	}

    private static void readFile(String fileName) {
    	File file = new File(fileName);
    	BufferedReader reader = null;
        try {
            System.out.println("文件内容：");
            reader = new BufferedReader(new InputStreamReader( new FileInputStream(file),"utf-8"));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
           while ((tempString = reader.readLine()) != null) {
				System.out.println(tempString);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
	}	

}
